package com.sakura.mall.coupon.skuFullReduction.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.skuFullReduction.domain.entity.SkuFullReductionEntity;
import com.sakura.mall.coupon.skuFullReduction.domain.request.AddSkuFullReductionRequest;
import com.sakura.mall.coupon.skuFullReduction.domain.request.UpdateSkuFullReductionRequest;
import com.sakura.mall.coupon.skuFullReduction.domain.request.SkuFullReductionPageRequest;
import com.sakura.mall.coupon.skuFullReduction.domain.vo.SkuFullReductionVo;
import com.sakura.mall.coupon.skuFullReduction.mapper.SkuFullReductionMapper;
import com.sakura.mall.coupon.skuFullReduction.service.SkuFullReductionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品满减信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReductionEntity> implements SkuFullReductionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSkuFullReductionRequest request) {
        // todo 其他校验逻辑
        SkuFullReductionEntity skuFullReduction = request.convert(SkuFullReductionEntity.class);
        this.save(skuFullReduction);
    }

    @Override
    public Page<SkuFullReductionVo> queryPage(SkuFullReductionPageRequest request) {
        IPage<SkuFullReductionEntity> qp = request.toPage();
        LambdaQueryWrapper<SkuFullReductionEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SkuFullReductionVo> ret = qp.convert(entity -> entity.convert(SkuFullReductionVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSkuFullReductionRequest request) {
        SkuFullReductionEntity entity = request.convert(SkuFullReductionEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

